package com.easemob.cases.ui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.easemob.cases.MessageHelper;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.easeui.widget.EaseChatMessageList;
import com.hyphenate.easeui.widget.chatrow.EaseChatRow;
import com.wechat.android.app.R;
import com.wechat.android.app.utils.JsonUtil;
import com.wechat.android.app.utils.MyLog;
import com.wechat.android.app.utils.PhoneUtils;
import com.wechat.android.app.utils.glide.GlideUtils;


/**
 * Created by hedepu on 2017/12/15 0013.
 */
public class ChatRowCustomerService extends EaseChatRow {
    private TextView tvPrice;
    private ImageView packageLogo;
    private TextView contentView;

    public ChatRowCustomerService(Context context, EMMessage message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    public void setUpView(EMMessage message, int position, EaseChatMessageList.MessageListItemClickListener itemClickListener) {
        super.setUpView(message, position, itemClickListener);
    }

    @Override
    protected void setMessageSendCallback() {
        super.setMessageSendCallback();
    }

    @Override
    protected void setMessageReceiveCallback() {
        super.setMessageReceiveCallback();
    }


    @Override
    protected void onInflatView() {
        inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                R.layout.ease_row_received_customer_service : R.layout.ease_row_received_customer_service, this);
    }

    @Override
    protected void onFindViewById() {
        contentView = (TextView) findViewById(R.id.tv_chatcontent);
        packageLogo = (ImageView) findViewById(R.id.iv_package_logo);
        tvPrice = (TextView) findViewById(R.id.tv_price);
    }

    @Override
    protected void onUpdateView() {
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onSetUpView() {
        EMTextMessageBody txtBody = (EMTextMessageBody) message.getBody();
//        Spannable span = EaseSmileUtils.getSmiledText(context, txtBody.getMessage());
//        // 设置内容
//        contentView.setText(span, TextView.BufferType.SPANNABLE);
//        contentView.setText(txtBody.getMessage());
        String type = message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, null);
        MyLog.e("yang", "type==" + type);
        String data = message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null);
        MyLog.e("yang", "data==" + data);
        //Log.i("King", "MESSAGE_EXT_CUSTOM_TYPE:" + message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE,null));
        if ("goods".equals(type)) {
            try {
                Log.i("King", JsonUtil.getJsonValuesString(data, "logo"));
                GlideUtils.loadImage(context, JsonUtil.getJsonValuesString(data, "logo"), packageLogo, null, R.mipmap.icon_goods_list_default, R.mipmap.icon_goods_list_default);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        tvPrice.setText("¥" + JsonUtil.getJsonValuesString(data, "price"));
        contentView.setText(JsonUtil.getJsonValuesString(data, "goodsName"));
        packageLogo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBubbleClick();
            }
        });
    }


    @Override
    protected void onBubbleClick() {
        // TODO 跳转至门店套餐详情页购买
        try {
            String type = message.getStringAttribute(MessageHelper.MESSAGE_EXT_CUSTOM_TYPE, null);
            String data = message.getStringAttribute(MessageHelper.MESSAGE_EXT_DATA, null);

            Intent intent  = new Intent();
            //MyLog.e("yang","data=="+data);
            switch (type){
                case "package":
//                    int servicePackageId = JsonUtil.getJsonValuesInt(data,"servicePackageId");
//                    int pushPackageId = JsonUtil.getJsonValuesInt(data,"pushPackageId");
//                    intent.setClass(context, PushPackageCardDetailActivity.class);
//                    intent.putExtra("servicePackageId",servicePackageId);
//                    intent.putExtra("pushPackageId",pushPackageId);
                    break;
                case "goods":
//                    intent.setClass(context, GoodsDetailActivity.class);
//                    intent.putExtra("goodsId", JsonUtil.getJsonValuesInt(data,"goodsId"));
                    break;
            }
            //MyLog.e("yang","servicePackageId=="+servicePackageId+"\npushPackageId==="+pushPackageId);
            context.startActivity(intent);
        }catch (Exception e){
            PhoneUtils.ShowToastMessage(context,"参数异常");
        }

    }


}
